﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using LowLevelGraphics;
using LowLevelGraphics.Filter;
using LowLevelGraphics.Forms;
using System.Windows.Forms;


namespace ImageRecognition2.Action
{
    /// <summary>
    /// Action parameter
    /// </summary>
    public class ActionParameter : AbstractCommand
    {
        //private PictureBox m_PictureBox = null;
        private Type m_Type = typeof(void);
        private object[] m_aParam = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="ActionParameter"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        public ActionParameter(Type type)
            : this(type, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ActionParameter"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="args">The args.</param>
        public ActionParameter(Type type, params object[] args)
        {
            m_aParam = args;
            m_Type = type;
        }

        /// <summary>
        /// Show a parameter input type form
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public override void Run(object sender, EventArgs e)
        {
            FormParameter parameter = new FormParameter();
            if (parameter.ShowDialog() == DialogResult.OK)
            {
                UnsafeBitmap bitmap = MainFormSingleton.ActivePictureBox.BackgroundImage;

                IFilter filter = null;
                
                if (m_aParam != null)
                {
                    filter = (IFilter)Activator.CreateInstance(m_Type, m_aParam[0], parameter.nValue);
                }
                else
                {
                    filter = (IFilter)Activator.CreateInstance(m_Type, (sbyte)parameter.nValue);
                }
                UnsafeBitmap unsafeBitmap = filter.Execute(bitmap);
                unsafeBitmap.Dispose();
            }
        }
    }
}
